package cn.laixueit.controller;

import cn.laixueit.client.StudentClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/student")
//@DefaultProperties(defaultFallback = "fallbackReturn")
public class StudentController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/getname")
//    @HystrixCommand(fallbackMethod = "fallbackReturn")
//    @HystrixCommand
    public String getname(){
//        List<ServiceInstance> instances = discoveryClient.getInstances("PROVIDERAPP");
//        ServiceInstance instance = instances.get(0);
//        System.out.println(instance.getHost());
//        System.out.println(instance.getPort());
//        System.out.println(instance.getInstanceId());
//        System.out.println(instance.getServiceId());
//        System.out.println(instance.getUri());
        String name = restTemplate.getForObject("http://PROVIDERAPP/student/getname", String.class);
        return name;
    }

    @RequestMapping("/getname2")
//    @HystrixCommand(fallbackMethod = "fallbackReturn2")
    public String getname2(){
//        List<ServiceInstance> instances = discoveryClient.getInstances("PROVIDERAPP");
//        ServiceInstance instance = instances.get(0);
//        System.out.println(instance.getHost());
//        System.out.println(instance.getPort());
//        System.out.println(instance.getInstanceId());
//        System.out.println(instance.getServiceId());
//        System.out.println(instance.getUri());
        String name = restTemplate.getForObject("http://PROVIDERAPP/student/getname", String.class);
        return name;
    }

    public String fallbackReturn(){

        return "当前访问人数较多。。。请稍后再试";
    }

    public String fallbackReturn2(){

        return "当前访问人数较多。。。请稍后再试2";
    }

    @Autowired
    private StudentClient studentClient;

    @RequestMapping("/name")
    public String name(){
        String getname = studentClient.getname();
        return getname;
    }

}
